package com.cn.jdia.springmvc.handlers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;

@Controller
public class AgentController {

	@RequestMapping("/agentctrl")
	public String getAgentInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String start = request.getParameter("start");
		String end = request.getParameter("end");
		start = start.replaceAll("-", "");
		end = end.replaceAll("-", "");

		// System.out.println(start);
		// System.out.println(end);

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("springmvc.xml");
		DataSource ds = (DataSource) w_context.getBean("dataSource");

		try {
			Connection conn = ds.getConnection();
			Statement st = conn.createStatement();

			// 现在有身份证号码最后一位大小写的问题
			StringBuffer sb = new StringBuffer("select attendance_time cdate, ")
					// .append(",agent_user_name,agent_id_card,create_id_card,remark")
					.append("(select dept_name dept1 from jan_person_cast where identity_id=agent_id_card or indentity_id_sms=agent_id_card LIMIT 1),")
					.append("(select dept_name dept2 from jan_person_cast where identity_id=create_id_card or indentity_id_sms=create_id_card LIMIT 1)")
					.append(" from tm_sms_attendance_agent where to_char(attendance_time,'YYYYMMDD') >= '")
					.append(start).append("' and to_char(attendance_time,'YYYYMMDD') <= '").append(end).append("'");
			// System.out.println(sb.toString());

			ResultSet rs = st.executeQuery(sb.toString());
			while (rs.next()) {
				String date = rs.getString("cdate");
				String dept1 = rs.getString("dept1");
				String dept2 = rs.getString("dept2");

				if ((dept1 == null) && (dept2 == null)) {
					continue;
				}

				// System.out.println(date+", " +dept1+","+dept2);

				Map<String, String> map = new HashMap<>();
				map.put("date", date);
				if (dept1 != null) {
					map.put("dept", dept1);
				} else {
					map.put("dept", dept2);
				}

				list.add(map);
			}

			st.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Map<String, Integer> count = new HashMap<>();

		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = list.get(i);
			//System.out.println(map.get("date") + " , " + map.get("dept"));

			if (!count.containsKey(map.get("dept"))) {
				count.put(map.get("dept"), 1);
			} else {
				int ic = count.get(map.get("dept"));
				ic++;
				count.put(map.get("dept"), ic);
			}
		}

		Gson gson = new Gson();
		String s = gson.toJson(count);
		// System.out.println(s);

		out.write(s);
		out.flush();
		out.close();

		return null;
	}
}
